/*
 * The RavenX Library
 * Copyright (c) 2008 by Solidus
 * 
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.It is provided "as is" without express 
 * or implied warranty.
 *
 */

#include "rx_typedef.h"
#include "rx_misc.h"
#include "rx_mem.h"
#include "rx_debug.h"

RX_SPACE_BEG





static void			rx_default_init_func(void *ptr, size_t elem_size)
{
		rx_memset(ptr, 0, elem_size);
}

static void			rx_default_uninit_func(void *ptr, size_t elem_size)
{
		rx_memset(ptr, 0, elem_size);
}

static void rx_default_copy_func(void *dest, const void *sour, size_t elem_size)
{
		rx_memcpy(dest, sour, elem_size);
}

static int		rx_default_cmp_func(const void *left, const void *right, size_t elem_size)
{
		 return rx_memcmp(left, right, elem_size);
}

static const void*	rx_default_key_func(const void *ptr)
{
		return ptr;
}

static rx_uint_n rx_default_hash_func(const void *data, size_t elem_size)
{
		return rx_hash_byte((const rx_byte*)data, elem_size);

}

void rx_init_info(rx_info_t *pinfo)
{
		rx_memset(pinfo, 0, sizeof(rx_info_t));
}



void rx_install_info(rx_info_t *pinfo)
{
#define __INFO_SET(sn, dn) do{if(pinfo->sn == NULL)pinfo->sn = dn;}while(0)

		if(pinfo == NULL)return;
		RX_ASSERT(pinfo->elem_size > 0);
		__INFO_SET(init_f, rx_default_init_func);
		__INFO_SET(uninit_f, rx_default_uninit_func);
		__INFO_SET(copy_f, rx_default_copy_func);
		__INFO_SET(cmp_f, rx_default_cmp_func);
		__INFO_SET(key_f, rx_default_key_func);
		__INFO_SET(hash_f, rx_default_hash_func);

#undef __INFO_SET
}




#define __rx_default_info(pinfo, size)do{ rx_init_info((pinfo)); (pinfo)->elem_size = (size); rx_install_info((pinfo));}while(0)

rx_info_t* rx_default_info(rx_info_t *pinfo, size_t size)
{
		__rx_default_info(pinfo, size);
		return pinfo;
}

static const rx_info_t __static_info = 
{
		sizeof(void*),
		0,
		rx_default_init_func,
		rx_default_uninit_func,
		rx_default_copy_func,
		rx_default_cmp_func,
		rx_default_key_func,
		NULL,
		NULL,
};

const rx_info_t* rx_static_info()
{
		return &__static_info;

}

RX_SPACE_END

